var twbMonthOrderVm = new Vue({
    el: '#rrapp',
    data: {
        /*订单信息*/
        orderData:[],
        /*销售总金额*/
        totalMoney:0,
        /*所有选中订单行*/
        selectedOrderList:[],
        /*勾选订单数量*/
        orderCount:0,
        /*打开收银框*/
        monthMoneyVisible:false,
        //支付方式
        payTypeData:[],
        /*订单收银*/
        moneyForm:{payableAmount:0,orderCount:0,payment:'现金',memberName:'',orderNos:''},
        formLabelWidth: '120px',
        q: {
            orderNo: '', //订单号
            memberName: '', //客户名称
            projectName:'',//项目名称
            productionStatus: '',//订单状态
            payStatus:'', //付款状态
            shipStatus:'',
            startTime: '',
            endTime: '',
            //查询取消订单标记
            isCancel:0,
        },
        e:{
            //订单状态:
            productionStatusEnum:[],
            //支付状态:
            payStatusEnum:[],
        },
        page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0, ascending: [], descending: []},
        title: null,
    },
    mounted: function () {
        this.query();
    },
    methods: {
        // 页数变化
        handleSizeChange: function (val) {
            this.page.pageSize = val;
            this.reload();
        },
        //当前页变化
        handleCurrentChange: function (val) {
            this.page.currentPage = val;
            this.reload();
        },
        // 查询
        query: function () {
            //查询参数
            var queryParam = this.q;
            this.page={currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0};
            // 分页参数
            queryParam.page = this.page.currentPage;
            queryParam.limit = this.page.pageSize;
            //ajax 请求
            var self= this;
            $.ajax({
                type: "POST",
                url: baseURL + "twbOrder/erporder/monthList",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(queryParam),
                success: function (r) {
                    if (r.code == 0) {
                        self.page.total = r.page.totalCount;
                        self.orderData = r.page.list;
                        self.e.productionStatusEnum=r.productionStatusEnum;
                        self.e.payStatusEnum=r.payStatusEnum;
                        self.e.orderSourceEnum=r.orderSourceEnum;
                        self.totalMoney=r.totalMoney;
                    } else {
                        alert(r.msg);
                    }
                }
            });

        },
        //重置查询条件:
        reset() {
                 this.q.orderNo = '',
                 this.q.memberName = '',
                 this.q.shipName = '',
                 this.q.shipTel = '',
                 this.q.orderSource = '',
                 this.q.productionStatus = '',
                 this.q.startTime = '',
                 this.q.endTime = '',
                 this.q.projectName='',
                 this.q.isCancel=0;
                 this.query();
        },
        reload:function(){
            //查询参数
            var queryParam = this.q;
            // 分页参数
            queryParam.page = this.page.currentPage;
            queryParam.limit = this.page.pageSize;
            var self=this;
            //ajax 请求
            $.ajax({
                type: "POST",
                url: baseURL + "twbOrder/erporder/monthList",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(queryParam),
                success: function (r) {
                    if (r.code == 0) {
                        self.page.total = r.page.totalCount;
                        self.orderData = r.page.list;
                        self.e.productionStatusEnum=r.productionStatusEnum;
                        self.e.payStatusEnum=r.payStatusEnum;
                        self.totalMoney=r.totalMoney;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        //订单详情
        erpOrderDetail:function(orderNo){
            window.parent.vm.zyRouter({
                url:'modules/order/twbOrderDetail.html',
                name:'订单详情',
                blank: true,
                vmName:'twbOrderDetailVm',
                params:{orderNo:orderNo,type:2},
            });
        },
        //打开收银向后台发送请求收银参数
        openReceviceMoney:function () {
            var self=this;
            if(self.selectedOrderList.length==0){
                alert("请至少选中一行再进行<span style='color: red;'>订单收银</span>操作!");
                return;
            }
            var ids=[];
            for (var obj in self.selectedOrderList) {
                ids.push(self.selectedOrderList[obj].orderNo);
            }
            var orderNos=ids.join(',');
            $.get(baseURL + "twbOrder/erporder/getMonthMoneyData?orderNos="+ orderNos, function(r) {
                if (r.code == 0) {
                    self.moneyForm=r.moneyForm;
                    self.payTypeData=r.payTypeData;
                    self.monthMoneyVisible=true;
                } else {
                    alert(r.msg);
                }
            });
        },
        //取消订单收银
        cancel:function(){
            var self=this;
                this.$confirm('是否确认取消收款?','收款取消提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                self.monthMoneyVisible=false;
                //清空数据
                self.moneyForm={payableAmount:0,orderCount:0,payment:'现金',memberName:''};
            }).catch(function () {
                this.$message({
                    type: 'info',
                    message: '已取消操作'
                })
            });
        },
        //确认收银
        save:function(){
            var self=this;
            $.ajax({
                type: "POST",
                url: baseURL + "twbOrder/erporder/saveMonthMoney",
                contentType: "application/json",
                data: JSON.stringify(self.moneyForm),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            self.monthMoneyVisible=false;
                            self.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        //获取所有选中行
        handleOrderSelection:function(val){
            twbMonthOrderVm.selectedOrderList = val;
        },
        /*格式化生产状态*/
        formatterProdStatus:function (row, column) {
            for (var i=0;i<this.e.productionStatusEnum.length;i++){
                if(row.productionStatus==this.e.productionStatusEnum[i].id){
                    return this.e.productionStatusEnum[i].describe;
                }
            }
        },
        /*格式化付款状态*/
        formatterPayStatus:function (row, column) {
            for (var i=0;i<this.e.payStatusEnum.length;i++){
                if(row.payStatus==this.e.payStatusEnum[i].value){
                    return this.e.payStatusEnum[i].describe;
                }
            }
        },
        //从收银跳转到此页面
        loadData(data){
            var orderNo=data.orderNo;
            if(orderNo){
                this.q.orderNo=orderNo;
            }
            this.query();
        }
    },
});